package com.jwdf.uda.ui.activity;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.view.banner.GalleryIndicator;
import pl.droidsonroids.gif.GifImageView;
/**
 * Created by fatsfan on 2016/11/22.
 */

public class LaunchActivity extends AbsActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay_launch);
        ViewPager vp = (ViewPager) findViewById(R.id.vp);
        final Adapter adapter = new Adapter();
        vp.setAdapter(adapter);

        final GalleryIndicator vIndicator = (GalleryIndicator) findViewById(R.id.indicator);
        vIndicator.setCount(4);

        final View vSkip = findViewById(R.id.vSkip);
        vSkip.setOnClickListener(this);

        final View vInto = findViewById(R.id.vInto);
        vInto.setOnClickListener(this);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(position == adapter.getCount() - 1) {
                    vSkip.setVisibility(View.INVISIBLE);
                    vInto.setVisibility(View.VISIBLE);
                    vIndicator.setVisibility(View.INVISIBLE);
                } else {
                    vSkip.setVisibility(View.VISIBLE);
                    vInto.setVisibility(View.INVISIBLE);
                    vIndicator.setVisibility(View.VISIBLE);
                }

                vIndicator.setSeletion(position);
            }
            @Override
            public void onPageSelected(int position) {
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    protected boolean fullScreenMode() {
        return true;
    }

    @Override
    protected boolean lightStatusBar() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.vSkip:
                startLogin();
                break;

            case R.id.vInto:
                startLogin();
                break;
        }
    }
    private void startLogin() {
        //进入到主页面后就不会再次打开引导页面
        ConfigUtils.setIsLaunch(LaunchActivity.this);
        startNewActivity(MainLoginEntryActivity.class);
        finish();
    }
    class Adapter extends PagerAdapter {

        GifImageView[] vGifArr;

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        public Adapter() {
            vGifArr = new GifImageView[4];

            for(int i = 0; i < vGifArr.length; i++)
                vGifArr[i] = new GifImageView(LaunchActivity.this);

            try {
                // 如果加载的是gif动图，第一步需要先将gif动图资源转化为GifDrawable
                // 将gif图资源转化为GifDrawable
                // gif1加载一个动态图gif
                vGifArr[0].setBackgroundResource(R.mipmap.guide_0);


                // 如果是普通的图片资源，就像Android的ImageView set图片资源一样简单设置进去即可。
                // gif2加载一个普通的图片（如png，bmp，jpeg等等）
                vGifArr[1].setBackgroundResource(R.mipmap.guide_1);

                vGifArr[2].setBackgroundResource(R.mipmap.guide_2);

                vGifArr[3].setBackgroundResource(R.mipmap.guide_3);


            } catch(Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public int getCount() {
            return vGifArr.length;
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(vGifArr[position]);
            return vGifArr[position];
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

}
